package com.aba.hos.admin;
import java.util.ArrayList;
import java.util.List;

public class AdminManager {
    private List<Admin> adminList;

    public AdminManager() {
        this.adminList = new ArrayList<>();
    }

    public AdminManager(List<Admin> adminList) {
        this.adminList = adminList;
    }

    // 添加管理员
    public boolean addAdmin(Admin admin) {
        // 检查管理员ID是否已存在
        for (Admin a : adminList) {
            if (a.getAdminId().equals(admin.getAdminId())) {
                System.out.println("错误：管理员ID已存在！");
                return false;
            }
        }
        adminList.add(admin);
        System.out.println("管理员添加成功：" + admin.getName());
        return true;
    }

    // 删除管理员
    public boolean deleteAdmin(String adminId) {
        Admin adminToRemove = null;
        for (Admin admin : adminList) {
            if (admin.getAdminId().equals(adminId)) {
                adminToRemove = admin;
                break;
            }
        }

        if (adminToRemove != null) {
            adminList.remove(adminToRemove);
            System.out.println("管理员删除成功：" + adminToRemove.getName());
            return true;
        } else {
            System.out.println("错误：未找到该管理员！");
            return false;
        }
    }

    // 修改管理员信息
    public boolean updateAdmin(Admin updatedAdmin) {
        for (int i = 0; i < adminList.size(); i++) {
            Admin admin = adminList.get(i);
            if (admin.getAdminId().equals(updatedAdmin.getAdminId())) {
                adminList.set(i, updatedAdmin);
                System.out.println("管理员信息更新成功：" + updatedAdmin.getName());
                return true;
            }
        }
        System.out.println("错误：未找到该管理员！");
        return false;
    }

    // 根据ID查找管理员
    public Admin findAdminById(String adminId) {
        for (Admin admin : adminList) {
            if (admin.getAdminId().equals(adminId)) {
                return admin;
            }
        }
        System.out.println("未找到管理员ID为 " + adminId + " 的记录");
        return null;
    }

    public Admin findAdminByName(String name) {
        for (Admin admin : adminList) {
            if (admin.getUsername().equals(name)) {
                return admin;
            }
        }
        System.out.println("未找到管理员ID为 " + name + " 的记录");
        return null;
    }

    // 获取所有管理员
    public List<Admin> getAllAdmins() {
        return adminList;
    }

    // 管理员登录验证
    public Admin authenticate(String username, String password) {
        for (Admin admin : adminList) {
            if (admin.getUsername().equals(username) && admin.getPassword().equals(password)) {
                return admin;
            }
        }
        return null;
    }
}    